/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import presentation.controller.SubjectPanelControl;
import vo.*;

/**
 *
 * @author Joanna 8a
 */
public class SubjectPanel extends javax.swing.JPanel {

    public static final int UPDATE_SUBJECT = 1;
    public static final int ADD_SUBJECT = 2;
    public static final int DELETE_SUBJECT = 3;
    private Font font = new Font("Segoe UI Light", 0, 14);
    private Font fontTitle = new Font("Segoe UI Light", 1, 14);
    private Color color = new Color(204, 0, 204);

    /**
     * Creates new form PanelAñadirMateria
     */
    public SubjectPanel(AuxiliarFrame parent, int type) {
        initComponents();
        this.parent = parent;
        jSpinnerCredits.setModel(new javax.swing.SpinnerNumberModel(1, 1, 20, 1));
        switch (type) {
            case ADD_SUBJECT:
                jComboBoxSubjects.setVisible(false);
                jTextFieldSubjectName.setVisible(true);
                setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adición de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, fontTitle, color)); // NOI18N
                jButtonAddSubject.setText("Añadir Materia");
                parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Computer.png")).getImage());
                break;
            case UPDATE_SUBJECT:
                jComboBoxSubjects.setVisible(true);
                jTextFieldSubjectName.setVisible(false);
                setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Modificación de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, fontTitle, color)); // NOI18N
                jButtonAddSubject.setText("Modificar Materia");
                parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Folder2.png")).getImage());
                List<SubjectVO> subjects = new SubjectPanelControl().getSubjects();
                for (SubjectVO sub : subjects) {
                    jComboBoxSubjects.addItem(sub);
                }
                jTextFieldSubjectCode.setEnabled(false);
                jSpinnerCredits.setEnabled(false);
                jButtonAddGroup.setEnabled(false);
                jButtonAddTeacher.setEnabled(false);
                jButtonSaveGroups.setEnabled(false);
                jButtonRemoveGroup.setEnabled(false);
                jButtonAddSubject.setEnabled(false);
                if (jComboBoxSubjects.getSelectedItem() != null) {
                    jComboBoxSubjectsActionPerformed(null);
                }
                break;
            case DELETE_SUBJECT:
                jComboBoxSubjects.setVisible(true);
                jTextFieldSubjectName.setVisible(false);
                setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Eliminación de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, fontTitle, color)); // NOI18N
                jButtonAddSubject.setText("Eliminar Materia");
                parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Folder3.png")).getImage());
                List<SubjectVO> subject = new SubjectPanelControl().getSubjects();
                for (SubjectVO sub : subject) {
                    jComboBoxSubjects.addItem(sub);
                }
                jTextFieldSubjectCode.setEnabled(false);
                jSpinnerCredits.setEnabled(false);
                jTableGroups.setEnabled(false);
                jTableClassDays.setEnabled(false);
                jButtonAddGroup.setEnabled(false);
                jButtonAddTeacher.setEnabled(false);
                jButtonSaveGroups.setEnabled(false);
                jButtonRemoveGroup.setEnabled(false);
                jButtonAddSubject.setEnabled(false);
                if (jComboBoxSubjects.getSelectedItem() != null) {
                    jComboBoxSubjectsActionPerformed(null);
                }
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonAddSubject = new javax.swing.JButton();
        jTextFieldSubjectCode = new javax.swing.JTextField();
        jLabelSubjectCode = new javax.swing.JLabel();
        jTextFieldSubjectName = new javax.swing.JTextField();
        jLabelSubjectName = new javax.swing.JLabel();
        jPanelGroups = new javax.swing.JPanel();
        jButtonSaveGroups = new javax.swing.JButton();
        jButtonAddGroup = new javax.swing.JButton();
        jButtonRemoveGroup = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTableGroups = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableClassDays = new javax.swing.JTable();
        jButtonAddTeacher = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSpinnerCredits = new javax.swing.JSpinner();
        jButtonCancel = new javax.swing.JButton();
        jComboBoxSubjects = new javax.swing.JComboBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adición de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, fontTitle, color));

        jButtonAddSubject.setFont(font);
        jButtonAddSubject.setText("Añadir Materia");
        jButtonAddSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddSubjectActionPerformed(evt);
            }
        });

        jTextFieldSubjectCode.setFont(font);

        jLabelSubjectCode.setFont(font);
        jLabelSubjectCode.setText("Código De La Materia");

        jTextFieldSubjectName.setFont(font);

        jLabelSubjectName.setFont(font);
        jLabelSubjectName.setText("Nombre de la Materia");

        jPanelGroups.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Grupos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, font));

        jButtonSaveGroups.setFont(font);
        jButtonSaveGroups.setText("Guardar Datos del Grupo");
        jButtonSaveGroups.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveGroupsActionPerformed(evt);
            }
        });

        jButtonAddGroup.setFont(font);
        jButtonAddGroup.setText("Añadir Grupo");
        jButtonAddGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddGroupActionPerformed(evt);
            }
        });

        jButtonRemoveGroup.setFont(font);
        jButtonRemoveGroup.setText("Quitar Grupo");
        jButtonRemoveGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveGroupActionPerformed(evt);
            }
        });

        jTableGroups.setFont(font);
        jTableGroups.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Grupo", "Número de Dias De Clase"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableGroups.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableGroupsMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(jTableGroups);

        jTableClassDays.setFont(font);
        jTableClassDays.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "# Grupo", "Salón", "Día", "Hora", "Duración", "Profesor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableClassDays);

        jButtonAddTeacher.setFont(font);
        jButtonAddTeacher.setText("Añadir Profesor");
        jButtonAddTeacher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddTeacherActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelGroupsLayout = new javax.swing.GroupLayout(jPanelGroups);
        jPanelGroups.setLayout(jPanelGroupsLayout);
        jPanelGroupsLayout.setHorizontalGroup(
            jPanelGroupsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
            .addGroup(jPanelGroupsLayout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(jButtonAddGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonRemoveGroup)
                .addGap(110, 110, 110))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelGroupsLayout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(jButtonAddTeacher)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonSaveGroups)
                .addGap(67, 67, 67))
        );
        jPanelGroupsLayout.setVerticalGroup(
            jPanelGroupsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGroupsLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelGroupsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddGroup)
                    .addComponent(jButtonRemoveGroup))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanelGroupsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddTeacher)
                    .addComponent(jButtonSaveGroups))
                .addGap(217, 217, 217))
        );

        jLabel1.setFont(font);
        jLabel1.setText("Número de Créditos");

        jButtonCancel.setFont(font);
        jButtonCancel.setText("Cancelar");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jComboBoxSubjects.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxSubjectsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanelGroups, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldSubjectName, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                                    .addComponent(jLabelSubjectCode)
                                    .addComponent(jTextFieldSubjectCode, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jSpinnerCredits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(50, 50, 50)
                                .addComponent(jButtonAddSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(68, 68, 68)
                                .addComponent(jButtonCancel))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabelSubjectName))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jComboBoxSubjects, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelSubjectName)
                .addGap(5, 5, 5)
                .addComponent(jComboBoxSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSubjectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelSubjectCode)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSubjectCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jSpinnerCredits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelGroups, javax.swing.GroupLayout.PREFERRED_SIZE, 479, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonAddSubject)
                    .addComponent(jButtonCancel)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddTeacherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddTeacherActionPerformed
        AuxiliarFrame aux = new AuxiliarFrame();
        aux.cambiarPanel(new PanelAddTeacher(aux));
        aux.setVisible(true);
    }//GEN-LAST:event_jButtonAddTeacherActionPerformed

    private void jButtonAddGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddGroupActionPerformed
        DefaultTableModel mod = (DefaultTableModel) jTableGroups.getModel();

        GroupVO vo = new GroupVO();
        if (mod.getRowCount() > 0) {
            vo.setGroupNumber((int) mod.getValueAt(mod.getRowCount() - 1, 0) + 1);
        } else {
            vo.setGroupNumber(1);
        }
        grupos.add(vo);

        TableColumn col1 = jTableGroups.getColumnModel().getColumn(1);
        SpinnerEditor spinner = new SpinnerEditor();
        col1.setCellEditor(spinner);
        spinner.stopCellEditing();

        Object[] row = new Object[2];
        row[0] = vo.getGroupNumber();
        row[1] = 1;
        mod.addRow(row);
    }//GEN-LAST:event_jButtonAddGroupActionPerformed

    private void jButtonRemoveGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveGroupActionPerformed
        DefaultTableModel mod = (DefaultTableModel) jTableGroups.getModel();
        if (mod.getRowCount() > 0) {
            mod.removeRow(filaGrupo);
            grupos.remove(filaGrupo);
        }
        filaGrupo = 0;
        DefaultTableModel mod2 = (DefaultTableModel) jTableClassDays.getModel();
        while (mod2.getRowCount() > 0) {
            mod2.removeRow(0);
        }
    }//GEN-LAST:event_jButtonRemoveGroupActionPerformed

    private void jTableGroupsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGroupsMouseClicked
        filaGrupo = jTableGroups.rowAtPoint(evt.getPoint());
        DefaultTableModel mod = (DefaultTableModel) jTableGroups.getModel();
        addRowsTable2((int) mod.getValueAt(filaGrupo, 1));
    }//GEN-LAST:event_jTableGroupsMouseClicked

    private void jButtonSaveGroupsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveGroupsActionPerformed
        saveGroupData();
    }//GEN-LAST:event_jButtonSaveGroupsActionPerformed

    private void jButtonAddSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddSubjectActionPerformed
        saveGroupData();
        switch (this.getAccessibleContext().getAccessibleName()) {
            case "Adición de Materias":
                SubjectVO vo = new SubjectVO();
                vo.setName(jTextFieldSubjectName.getText());
                vo.setCode(Integer.parseInt(jTextFieldSubjectCode.getText()));
                vo.setCredits((int) jSpinnerCredits.getValue());
                vo.setGroups(grupos);
                new SubjectPanelControl().crearMateria(vo);
                JOptionPane.showMessageDialog(this, "Materia agregada correctamente!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                break;
            case "Modificación de Materias":
                SubjectVO vo2 = ((SubjectVO) jComboBoxSubjects.getSelectedItem());
                vo2.setGroups(grupos);
                if (new SubjectPanelControl().updateSubject(vo2)) {
                    JOptionPane.showMessageDialog(this, "Materia modificada correctamente!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "La materia no se ha podido actualizar", "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "Eliminación de Materias":
                SubjectVO vo3 = ((SubjectVO) jComboBoxSubjects.getSelectedItem());
                if (new SubjectPanelControl().deleteSubject(vo3.getCode())) {
                    JOptionPane.showMessageDialog(this, "Materia Eliminada correctamente!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "La materia no se ha podido eliminar", "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;
        }
        parent.dispose();
    }//GEN-LAST:event_jButtonAddSubjectActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        parent.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jComboBoxSubjectsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxSubjectsActionPerformed
        grupos = ((SubjectVO) jComboBoxSubjects.getSelectedItem()).getGroups();
        jTextFieldSubjectCode.setText(new Integer(((SubjectVO) jComboBoxSubjects.getSelectedItem()).getCode()).toString());
        jSpinnerCredits.setValue(((SubjectVO) jComboBoxSubjects.getSelectedItem()).getCredits());
        DefaultTableModel mod = (DefaultTableModel) jTableGroups.getModel();
        int a = mod.getRowCount();
        for (int i = 0; i < a; i++) {
            mod.removeRow(0);
        }
        DefaultTableModel mod2 = (DefaultTableModel) jTableClassDays.getModel();
        int b = mod2.getRowCount();
        for (int i = 0; i < b; i++) {
            mod2.removeRow(0);
        }
        for (GroupVO group : grupos) {
            TableColumn col1 = jTableGroups.getColumnModel().getColumn(1);
            SpinnerEditor spinner = new SpinnerEditor();
            col1.setCellEditor(spinner);
            spinner.stopCellEditing();

            Object[] row = new Object[2];
            row[0] = group.getGroupNumber();
            row[1] = group.getClassDays().size();
            mod.addRow(row);
        }
        switch (this.getAccessibleContext().getAccessibleName()) {
            case "Modificación de Materias":
                jSpinnerCredits.setEnabled(true);
                jButtonAddGroup.setEnabled(true);
                jButtonAddTeacher.setEnabled(true);
                jButtonSaveGroups.setEnabled(true);
                jButtonRemoveGroup.setEnabled(true);
                jButtonAddSubject.setEnabled(true);
                break;
            case "Eliminación de Materias":
                jButtonAddSubject.setEnabled(true);
                break;
        }
    }//GEN-LAST:event_jComboBoxSubjectsActionPerformed

    private void addRowsTable2(int numFilas) {

        TableColumn col5 = jTableClassDays.getColumnModel().getColumn(5);
        JComboBox combo5 = new JComboBox();
        List<IValueObject> profesores = new SubjectPanelControl().obtenerProfesores();
        for (IValueObject prof : profesores) {
            combo5.addItem(((TeacherVO) prof).getName() + " " + ((TeacherVO) prof).getLastName());
        }
        col5.setCellEditor(new DefaultCellEditor(combo5));

        TableColumn col2 = jTableClassDays.getColumnModel().getColumn(2);
        JComboBox combo2 = new JComboBox(new javax.swing.DefaultComboBoxModel(new String[]{"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"}));
        col2.setCellEditor(new DefaultCellEditor(combo2));

        TableColumn col3 = jTableClassDays.getColumnModel().getColumn(3);
        JComboBox combo3 = new JComboBox(new javax.swing.DefaultComboBoxModel(new Double[]{7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0}));
        col3.setCellEditor(new DefaultCellEditor(combo3));

        DefaultTableModel mod = (DefaultTableModel) jTableClassDays.getModel();

        if (mod.getRowCount() > 0) {
            if (grupos.get(filaGrupo).getGroupNumber() == mod.getValueAt(0, 0)) {
                int filas = mod.getRowCount();
                int dif = numFilas - filas;
                if (dif > 0) {
                    Object[] fila = new Object[6];
                    for (int i = filas; i < numFilas; i++) {
                        fillRow(fila, i);
                    }
                } else {
                    for (int i = 0; i > dif; i--) {
                        mod.removeRow(mod.getRowCount() - 1);
                        grupos.get(filaGrupo).getClassDays().remove(mod.getRowCount() - 1);
                    }
                }
            } else {
                saveGroupData();
                int a = mod.getRowCount() - 1;
                for (int i = 0; i <= a; i++) {
                    mod.removeRow(0);
                }
                Object[] fila = new Object[6];
                for (int i = 0; i < numFilas; i++) {
                    fillRow(fila, i);
                }
            }
        } else {
            Object[] fila = new Object[6];
            for (int i = 0; i < numFilas; i++) {
                fillRow(fila, i);
            }
        }
    }

    private void fillRow(Object[] fila, int i) {
        DefaultTableModel mod = (DefaultTableModel) jTableClassDays.getModel();
        fila[0] = grupos.get(filaGrupo).getGroupNumber();
        List<ClassDayVO> dias = grupos.get(filaGrupo).getClassDays();
        if (dias.size() == i) {
            dias.add(new ClassDayVO());
        } else {
            fila[1] = dias.get(i).getClassRoom();
            fila[2] = dias.get(i).getDay();
            fila[3] = dias.get(i).getHour();
            fila[4] = dias.get(i).getSpan();
            TeacherVO profVo = new SubjectPanelControl().buscarProfesor(dias.get(i).getTeacherId());
            if (profVo != null) {
                fila[5] = profVo.getName() + " " + profVo.getLastName();
            }
        }
        mod.addRow(fila);
    }

    private void saveGroupData() {
        DefaultTableModel mod = (DefaultTableModel) jTableClassDays.getModel();
        int a = mod.getRowCount() - 1;
        for (int i = 0; i <= a; i++) {
            ClassDayVO vo = grupos.get((int) mod.getValueAt(0, 0) - 1).getClassDays().get(i);
            if (mod.getValueAt(i, 1) != null) {
                vo.setClassRoom((String) mod.getValueAt(i, 1));
            }
            if (mod.getValueAt(i, 2) != null) {
                vo.setDay((String) mod.getValueAt(i, 2));
            }
            if (mod.getValueAt(i, 3) != null) {
                if(! (mod.getValueAt(i, 3) instanceof Integer)){
                    double hour = (double) mod.getValueAt(i, 3);
                    vo.setHour((int) hour);
                }
            }
            if (mod.getValueAt(i, 4) != null) {
                if(! (mod.getValueAt(i, 4) instanceof Integer)){
                    double duration = (double) mod.getValueAt(i, 4);
                    vo.setSpan((int) duration);
                }
            }
            if (mod.getValueAt(i, 5) != null) {
                String[] profe = ((String) mod.getValueAt(i, 5)).split(" ");
                String nombre = profe[0];
                String apellido = profe[1];
                TeacherVO profeVO = new SubjectPanelControl().buscarProfesorPorNombre(nombre, apellido);
                vo.setTeacherId(profeVO.getId());
            }
        }
    }

    class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {

        private javax.swing.JSpinner spinner;

        public SpinnerEditor() {
            spinner = new JSpinner();
            SpinnerNumberModel spinnerModel = new javax.swing.SpinnerNumberModel(1, 1, 7, 1);
            spinner.setModel(spinnerModel);
            spinner.addChangeListener(new javax.swing.event.ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent e) {
                    filaGrupo = jTableGroups.rowAtPoint(spinner.getLocation());
                    addRowsTable2((int) spinner.getValue());
                }
            });
        }

        @Override
        public Object getCellEditorValue() {
            return spinner.getValue();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            return spinner;
        }
    }
    private List<GroupVO> grupos = new ArrayList<>();
    private int filaGrupo;
    private AuxiliarFrame parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddGroup;
    private javax.swing.JButton jButtonAddSubject;
    private javax.swing.JButton jButtonAddTeacher;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonRemoveGroup;
    private javax.swing.JButton jButtonSaveGroups;
    private javax.swing.JComboBox jComboBoxSubjects;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelSubjectCode;
    private javax.swing.JLabel jLabelSubjectName;
    private javax.swing.JPanel jPanelGroups;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSpinner jSpinnerCredits;
    private javax.swing.JTable jTableClassDays;
    private javax.swing.JTable jTableGroups;
    private javax.swing.JTextField jTextFieldSubjectCode;
    private javax.swing.JTextField jTextFieldSubjectName;
    // End of variables declaration//GEN-END:variables
}
